import { orderBy, uniq } from 'lodash-es'

export function uniqDesc(value: number[]) {
  return orderBy(uniq(value), (it) => it, 'desc')
}

/**
 *
 * @param value 需要排列后的值
 * @param max  最大值
 * @param min
 */
export function convertSymbolSize(value: number[], max = 40, min = 3) {
  return value.reduce<number[]>((res, cv, index, list) => {
    const pv = res[index - 1]
    if (index === 0) {
      res.push(max)
    } else if (pv <= 0 || cv <= 0) {
      res.push(0)
    } else {
      const digit = list[index - 1] / cv
      let size: number
      if (digit < 1) {
        size = pv - 1
      } else if (digit > 6) {
        size = pv - 12
      } else {
        size = pv - Math.floor(digit) * 2
      }
      res.push(size < min ? min : size)
    }
    return res
  }, [])
}

/**
 * 根据字节去切割内容，用于 tooltip 过长处理
 * @param input
 * @param byteLen
 */
export function byteSlice(input: string, byteLen = 40) {
  if (!input) {
    return ''
  }
  const strings = ['']
  let flag = 0
  for (let i = 0; i < input.length; i++) {
    if (flag > byteLen) {
      flag = 0
      strings.push('')
    }
    flag += input.charCodeAt(i) > 255 ? 2 : 1
    strings[strings.length - 1] = `${strings[strings.length - 1]}${input[i]}`
  }
  return strings.join('\n')
}
